﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using zichan.Common;

namespace CSVOS
{
    class Program
    {
        static void Main(string[] args)
        {
            string file1 = "C:\\Users\\admin\\Desktop\\1025(4).csv";
            //TestExcelRead(file);
           List<dataModel> list = CsvFileReader.Read(file1);
            DataTable table = ListToDataTable(list);
            string[] fields = { "adress","No","WanAn","FuWuShan","WebName","ZhuYuMing","Bip","Eip","DW1", "DW2", "Name", "Number", "Phone","Email","YY","YUMing","Pip","TrueOrNo","Remark" };
            string[] fieldsName = { "所属地","编号","接入地所属网安部门","接入服务商名称","网站名称","主域名","起始IP地址","结束IP地址","主办单位（个人）名称","主办单位（个人）地址","联系人姓名","办公电话","手机号码","电子邮件","核查原因","域名","ping出IP","是否为数字IP","是否我司IP"};
            ExcelHelper.Write2(table, "资产分类表", "资产分类表", fields, fieldsName, "资产分类表.xls");
        }
        public static DataTable ListToDataTable(IList list)
        {
            DataTable table = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    Type pt = pi.PropertyType;
                    if ((pt.IsGenericType) && (pt.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        pt = pt.GetGenericArguments()[0];
                    }
                    table.Columns.Add(new DataColumn(pi.Name, pt));
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    table.LoadDataRow(array, true);
                }
            }
            return table;
        }
    }
}
